package com.china08.yxyapi.api.home.kecheng.xiaoben;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;

/**
 * 校本课程--详情页--上边和左边
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenParticularSrhController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;

    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    // 章节
    @Autowired
    private YxyLessonsSchoolLessonChapterRepository yxyLessonsSchoolLessonChapterRepository;

    // 课时
    @Autowired
    private YxyLessonsSchoolLessonItemRepository yxyLessonsSchoolLessonItemRepository;

    /** 视频 */
    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/particular/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        // Boolean kechengVip = reqModel.kechengVip;
        // Boolean vip = reqModel.vip;
        RespModel model = new RespModel();

        YxyLessonsSchoolLesson yxyLessonsSchoolLessons = yxyLessonsSchoolLessonRepository.findById(lessonId);

        if (null != yxyLessonsSchoolLessons) {

            YxyLessonsSchoolCatalog yxyLessonsSchoolCatalog1 = yxyLessonsSchoolCatalogRepository
                    .findById(yxyLessonsSchoolLessons.getCatalogId1());
            YxyLessonsSchoolCatalog yxyLessonsSchoolCatalog2 = yxyLessonsSchoolCatalogRepository
                    .findById(yxyLessonsSchoolLessons.getCatalogId2());

            model.cover = yxyLessonsSchoolLessons.getCover();
            model.name = yxyLessonsSchoolLessons.getName();

            model.oneName = yxyLessonsSchoolCatalog1.getName();
            model.twoName = yxyLessonsSchoolCatalog2.getName();

            model.count = yxyLessonsSchoolLessons.getCount();
            model.duration = yxyLessonsSchoolLessons.getDuration();
            model.ep = yxyLessonsSchoolLessons.getEp();
            model.vv = yxyLessonsSchoolLessons.getVv();
            model.summary = yxyLessonsSchoolLessons.getSummary();
            model.author = yxyLessonsSchoolLessons.getAuthor();
            model.avator = yxyLessonsSchoolLessons.getAvator();
            model.kechengVip = yxyLessonsSchoolLessons.getVip() == 1 ? true : false;
        }
        // model.kechengVip = kechengVip;
        // model.vip = vip;
        List<RespModel> returnLst = new ArrayList<>();

        // 章节、课时
        List<Chapter> chapterList = new ArrayList<Chapter>();
        List<YxyLessonsSchoolLessonChapter> lessonChaptersLst = yxyLessonsSchoolLessonChapterRepository
                .findByLessonIdOrderByOrderNumAsc(lessonId);
        for (YxyLessonsSchoolLessonChapter yxyLessonsSchoolLessonChapter : lessonChaptersLst) {
            Chapter chapter = new Chapter();
            chapter.ChapterIntro = yxyLessonsSchoolLessonChapter.getIntro();
            chapter.ChapterTitle = yxyLessonsSchoolLessonChapter.getTitle();
            chapterList.add(chapter);

            List<LessonItem> lessonItemList = new ArrayList<LessonItem>();
            List<YxyLessonsSchoolLessonItem> lessonItemLst = yxyLessonsSchoolLessonItemRepository
                    .findByChapterIdAndStatusNotOrderByOrderNum(yxyLessonsSchoolLessonChapter.getId(), -1);
            for (YxyLessonsSchoolLessonItem yxyLessonsSchoolLessonItem : lessonItemLst) {
                LessonItem lessonItem = new LessonItem();

                // lessonItemId和userId
                // List<YxyLessonsSchoolLessonItem>
                // yxyLessonsSchoolLessonItemLst =
                // yxyLessonsSchoolLessonItemRepository.findByLessonId(lessonId);
                // for (YxyLessonsSchoolLessonItem yxyLessonsSchoolLessonItem2 :
                // yxyLessonsSchoolLessonItemLst) {

                // if (yxyLessonsVideos != null) {
                lessonItem.lessonItemId = yxyLessonsSchoolLessonItem.getId();
                YxyLessonsVideo yxyLessonsVideos = yxyLessonsVideoRepository
                        .findById(yxyLessonsSchoolLessonItem.getVideoId());
                if (null != yxyLessonsVideos) {
                    lessonItem.duration = yxyLessonsVideos.getDuration();
                }

                // }
                // }

                lessonItem.lessonItemIntro = yxyLessonsSchoolLessonItem.getIntro();
                lessonItem.lessonItemTitle = yxyLessonsSchoolLessonItem.getTitle();
                lessonItem.preview = yxyLessonsSchoolLessonItem.getPreview();
                lessonItem.lessonId = yxyLessonsSchoolLessonItem.getLessonId();
                lessonItem.OrderNum = yxyLessonsSchoolLessonItem.getOrderNum();
                lessonItemList.add(lessonItem);
            }
            chapter.lessonItemList = lessonItemList;
            // chapterList.add(chapter);
        }
        model.chapterList = chapterList;
        returnLst.add(model);
        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;
        // // 课程授权
        // public Boolean kechengVip;
        // // 已授权
        // public Boolean vip;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 封面
        public String cover;
        // 课程名称
        public String name;

        // ###
        // 一级分类名称
        public String oneName;
        // 二级分类名称
        public String twoName;

        // ###
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程评分（所有评分之和处于评分人数即综合评分）
        public Float ep;
        // 学习人数
        public Integer vv;
        // 课程简介
        public String summary;
        // 贡献者
        public String author;
        // 贡献者头像
        public String avator;

        // 课程授权
        public Boolean kechengVip;

        // ###
        public List<Chapter> chapterList;

    }

    /** 章节 */
    public static class Chapter {

        // 章节名称
        public String ChapterTitle;
        // 章节简介
        public String ChapterIntro;

        public List<LessonItem> lessonItemList;
    }

    /** 课时 */
    public static class LessonItem {

        // 课时名称
        public String lessonItemTitle;
        // 课时简介
        public String lessonItemIntro;
        // 免费试看
        public Integer preview;
        // 课程id
        public String lessonId;
        // 课时Id
        public String lessonItemId;
        // 视频时长
        public Integer duration;
        // 序号
        public Integer OrderNum;

    }
}
